package net.zjitc.redis7_study.demo;
/**
 * @Author 董熠涛
 * @Date 2025/4/3  1:01
 */


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.zjitc.redis7_study.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @Author:董熠涛
 * @ 2025/4/31:01
 */
@RunWith(SpringRunner.class) // JUnit4 需要此注解
@SpringBootTest // 关键注解，用于加载Spring上下文
public class JedisTemplateDemo {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
//    private List<RedisTemplate> redisTemplates;
    @Test
    public void test(){
//        RedisSerializer.string()采用string序列化
//        redisTemplate.opsForValue().set("sonName","dongertao");
//        System.out.println(redisTemplate.opsForValue().get("sonName"));
        User user = new User("dongyitao", 18);
        redisTemplate.opsForValue().set("user",user);
        User stringToUser = (User) redisTemplate.opsForValue().get("user");
        System.out.println(stringToUser);

//        查看redistemplate的个数以及其存在位置
//        System.out.println("RedisTemplate Beans: " + redisTemplates.size());
//
//        for (RedisTemplate redisTemplate : redisTemplates) {
//            System.out.println("RedisTemplate Class: " + redisTemplate.getClass().getName());
//        }
    }

    private static final ObjectMapper mapper = new ObjectMapper();
    @Test
//    stringRedisTemplate的使用
    public void test1() throws JsonProcessingException {
//        User user = new User("虎哥", 18);
//        String json = mapper.writeValueAsString(user);
//        stringRedisTemplate.opsForValue().set("user",json);
//
//        String jsonUser = stringRedisTemplate.opsForValue().get("user");
//        User jsonToUser = mapper.readValue(jsonUser, User.class);
//        System.out.println(jsonToUser);
    }
}
